#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2020/12/15 15:00
# @Author  : LuLu
# @FileName: tourguidePage.py
# @Software: PyCharm
from time import sleep
from page import basePage
from pagesLocators.navigationLocators import NavigationLoc
from pagesLocators.tourguideLocators import TourguideLocators as Loc
from selenium.webdriver.common.by import By


class TourguidePage(basePage.BasePage):
    # TODO 页面操作
    # 点击编辑
    def click_edit_button(self):
        self.click_element(Loc.edit_button)
        sleep(2)

    # 切换TAB
    def switch_Tab(self, TabName='基本信息'):
        try:
            if TabName == '基本信息' or TabName == 1:
                self.click_element(Loc.Tab_button(1))
            elif TabName == '线路攻略' or TabName == 2:
                self.click_element(Loc.Tab_button(2))
            elif TabName == '为食五部曲' or TabName == 3:
                self.click_element(Loc.Tab_button(3))
            elif TabName == '酒店' or TabName == 4:
                self.click_element(Loc.Tab_button(4))
            elif TabName == '避坑' or TabName == 5:
                self.click_element(Loc.Tab_button(5))
            elif TabName == '故事' or TabName == 6:
                self.click_element(Loc.Tab_button(6))
            elif TabName == '线路行程' or TabName == 7:
                self.click_element(Loc.Tab_button(7))
        except Exception as e:
            raise e
            print("切换tab失败")

    # 编辑器首次上传图片
    def click_editor_upload_first(self, *pic_name):
        try:
            self.click_element(Loc.editor_upload_button)
            sleep(3)
            self.switch_frame(Loc.editor_upload_iframe)
            self.uploading_picture(Loc.editor_upload_pic_button_first, *pic_name)
        except:
            self.refresh()
            self.click_editor_upload_first(*pic_name)

    # 编辑器继续上传图片，顶部按钮操作
    def click_editor_upload_continue_top(self, *pic_name):
        self.uploading_picture(Loc.editor_upload_pic_button_continue_top, *pic_name)

    # 编辑器继续上传图片，右侧按钮操作
    def click_editor_upload_continue_right(self, *pic_name):
        self.uploading_picture(Loc.editor_upload_pic_button_continue_right, *pic_name)

    # 编辑器点击开始上传
    def click_editor_upload_commit(self):
        self.click_element(Loc.editor_start_upload)

    # 已成功选择多少张
    def get_select_pic_length(self):
        pic_list = self.find_element(Loc.editor_selected_pic_list)
        return len(pic_list.find_elements_by_xpath('li')) - 1

    # 图片宽度限制
    def is_editor_upload_width_limit(self):
        pic_list = self.find_element(Loc.editor_selected_pic_list)
        try:
            pic_list.find_element(By.CLASS_NAME, 'state-cancelled')
            return False
        except:
            return True

    # 图片宽度限制
    def is_editor_upload_fail(self):
        pic_list = self.find_element(Loc.editor_selected_pic_list)
        try:
            pic_list.find_element(By.CLASS_NAME, 'state-error')
            return False
        except:
            return True
